package com.kinon.market.view;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.kinon.market.Constatus;
import com.kinon.market.R;
import com.kinon.market.data.DataModel;
import com.kinon.market.data.UPacketFactory;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import sunmi.ds.DSKernel;
import sunmi.ds.callback.IConnectionCallback;
import sunmi.ds.callback.IReceiveCallback;
import sunmi.ds.callback.ISendCallback;
import sunmi.ds.data.DSData;
import sunmi.ds.data.DSFile;
import sunmi.ds.data.DSFiles;
import sunmi.ds.data.DataPacket;

public class PayresultActivity extends Activity {

    String id;
    String token;
    String jine;
    String filePath = Environment.getExternalStorageDirectory().getPath() + "/dcim/chenggong.png";
    String pathBig = Environment.getExternalStorageDirectory().getPath() + "/dcim/chenggong.png";
    DSKernel mDSKernel;// SDK核心类
    DataPacket dsPacket;
    private TextView tv_jine;
    private TextView tv_fanhui;
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        public void run() {
            try {
                this.update();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            handler.postDelayed(this, 4000);// 3妙关闭
        }

        void update() throws IOException {
//			onrefresh.onPullDownToRefresh(list_order);
            finish();
//			handler.removeCallbacks(runnable);
            Log.i("zq", "finsh");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payresult);
        initRes();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    private void initRes() {
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        token = sp.getString("data", "");
        tv_jine = (TextView) findViewById(R.id.tv_jine);
        jine = getIntent().getStringExtra("jine");

        tv_fanhui = (TextView) findViewById(R.id.tv_fanhui);
        tv_fanhui.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Show();
        handler.postDelayed(runnable, 4000);
    }

    private void Show() {
        final DSKernel mDSKernel = DSKernel.newInstance();
        mDSKernel.init(getApplicationContext(), new IConnectionCallback() {
            @Override
            public void onDisConnect() {

            }

            @Override
            public void onConnected(ConnState state) {

            }
        });
        mDSKernel.addReceiveCallback(new IReceiveCallback() {
            @Override
            public void onReceiveData(DSData data) {

            }

            @Override
            public void onReceiveFile(DSFile file) {

            }

            @Override
            public void onReceiveFiles(DSFiles files) {

            }

            @Override
            public void onReceiveCMD(DSData cmd) {

            }
        });
        JSONObject json = new JSONObject();
        try {
            if (Constatus.URGENT_TYPE) {
                json.put("title", "");
                json.put("content", "支付成功");
            } else {
                json.put("title", "支付成功 ");
                json.put("content", "￥" + jine);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        mDSKernel.sendFile(DSKernel.getDSDPackageName(), json.toString(), "/storage/emulated/0/dcim/qing.png", new ISendCallback() {
            @Override
            public void onSendSuccess(long taskId) {
                String json = UPacketFactory.createJson(
                        DataModel.QRCODE, "");
                mDSKernel.sendCMD(DSKernel.getDSDPackageName(), json,
                        taskId, null);
            }

            @Override
            public void onSendFail(int errorId, String errorInfo) {

            }

            @Override
            public void onSendProcess(long totle, long sended) {

            }
        });
//
//		DataPacket packet = UPacketFactory.buildShowText(DSKernel.getDSDPackageName(), json.toString(),null);
//		mDSKernel.sendData(packet);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
    }
}
